package com.trkj.service.hjj;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.trkj.dao.hjj.EmppassDAO;
import com.trkj.dao.hjj.IFunctionDAO;
import com.trkj.dao.hjj.IFunctionTypeDAO;
import com.trkj.dao.hjj.IRoleDAO;
import com.trkj.dao.hjj.IUserDAO;
import com.trkj.pojo.Emppass;
import com.trkj.pojo.Function;
import com.trkj.pojo.FunctionType;
import com.trkj.pojo.Role;
import com.trkj.service.ServiceException;
import com.trkj.vo.EmppassVo;
import com.trkj.vo.FunctionExVo;
import com.trkj.vo.FunctionTypeExVo;

@Service
@Transactional(rollbackFor = Exception.class)
public class GrantService {
	public static final Long LOGIN_ID = 2L;
	
	@Autowired
	private IUserDAO userDAO;
	@Autowired
	private IRoleDAO roleDAO;
	@Autowired
	private IFunctionDAO functionDAO;
	@Autowired
	private IFunctionTypeDAO functionTypeDAO;
	@Autowired
	private EmppassDAO emppassDAO;
	/**
	 * 修改角色的名字
	 * @param roleId
	 * @param roleName
	 * @return
	 * @throws ServiceException
	 */
	public boolean updateRoleName(Long roleId,String roleName)throws ServiceException{
		try {
			Role role = roleDAO.findOne(roleId);
			role.setRoleName(roleName);
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
	/**
	 * 加载角色
	 * @param roleId
	 * @return
	 */
	public Role loadRole(Long roleId){
		return roleDAO.findOne(roleId);
	}
	/**
	 * 查询所有角色
	 * @return
	 */
	public List<Role> getAllRoles(){
		return roleDAO.findAll();
	}
	
	/**
	 * 添加新角色
	 * @param roleName
	 */
	public void addNewRole(String roleName){
		try {
			Role role = new Role();
			role.setRoleName(roleName);
			roleDAO.save(role);
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
	
	/**
	 * 删除用户
	 * @param roleId
	 */
	public void deleteRole(Long roleId){
		try {
			Role role = roleDAO.findOne(roleId);
			roleDAO.delete(role);
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
	/**
	 * 查询所有用户角色
	 * @param roleId
	 * @return
	 */
	public List<EmppassVo> queryByRole(Long roleId)throws ServiceException{
		try {
			List<Object[]> list = emppassDAO.queryByrole(roleId);
			List<EmppassVo> empvos = new ArrayList<>();
			
			if(list!=null){
				for (Object[] obj : list) {
					EmppassVo empvo = new EmppassVo();
					empvo.setPassId(((BigDecimal)obj[0]).longValue());
					empvo.setStainName(obj[1].toString());
					empvo.setState(((BigDecimal)obj[2]).longValue());
					empvos.add(empvo);
				}
				
			}
			return empvos;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
	/**
	 * 返回功能列表，并给当前角色拥有的功能设置选择状态
	 * @param roleId 角色id
	 * @return
	 */
	public List<FunctionTypeExVo> getFunctionTypeExs(long roleId){
		List<FunctionTypeExVo>funcTypeList = new ArrayList<FunctionTypeExVo>();
		//获取所有的分类
		List<FunctionType> types = (List<FunctionType>) functionTypeDAO.findAll();
		//循环分类，找出每个分类下所有的功能
		if(types != null){
			for(FunctionType type: types){
				FunctionTypeExVo functionTypeExVo = new FunctionTypeExVo();
				functionTypeExVo.setFtId(type.getFunctionId());
				functionTypeExVo.setFtName(type.getFunctionName());
				//获取当前分类下的功能列表
				List<Object[]> objsList = functionTypeDAO.findFunctionsByRoleId(roleId, type.getFunctionId());
				//将Object[]转换成FunctionExVo类型
				List<FunctionExVo> funsList = new ArrayList<FunctionExVo>();
				if(objsList != null){
					for(Object[] objs: objsList){
						FunctionExVo functionExVo = new FunctionExVo();
						functionExVo.setFunId(((BigDecimal)objs[0]).longValue());
						functionExVo.setFunName((String)objs[2]);
						functionExVo.setSelected(objs[3].equals(new BigDecimal(1)));
						funsList.add(functionExVo);
					}
					functionTypeExVo.setFunctions(funsList);
				}
				funcTypeList.add(functionTypeExVo);
			}
		}
		return funcTypeList;
	}
	
	/**
	 * 为角色添加用户
	 * @param roleId
	 * @param passId
	 */
	public void grantRole(long roleId, List<Long> passId)throws ServiceException{
		try {
			//通过角色id查找该角色
			Role role = roleDAO.findOne(roleId);
			if(role.getEmppasses()==null){
				role.setEmppasses(new ArrayList<Emppass>());
			}
			role.getEmppasses().clear();//先把用户全部清空
			if(passId!=null){
				for (Long long1 : passId) {
					Emppass emps = emppassDAO.findOne(long1);
					role.getEmppasses().add(emps);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
	/**
	 * 给用户授权
	 * @param roleId
	 * @param funId
	 */
	public void GrantRoleandFun(Long roleId,List<Long> funId){
		Role role = roleDAO.findOne(roleId);
		
		if(role.getFunctions()==null){
			role.setFunctions(new ArrayList<Function>());
		}
		role.getFunctions().clear();
		if(funId!=null){
			for (long long1 : funId) {
				Function fun = functionDAO.findOne(long1);
				role.getFunctions().add(fun);
			}
			
		}
	}
	/**
	 * 根据用户ID找到该用户所有的功能ID
	 * @param userId
	 * @return
	 */
	public Set<Long> getFunIdsByUserId(Long userId){
		Set<Long> funIds = new HashSet<Long>();
		Emppass user = userDAO.findOne(userId);
		List<Role> roleList = user.getRoles();
		if(roleList != null){
			for(Role role: roleList){
				List<Function> funList = role.getFunctions();
				if(funList != null){
					for(Function fun: funList){
						funIds.add(fun.getFunId());
					}
				}
			}
		}
		return funIds;
	}
	/**
	 * 修改角色名
	 * @param roleId
	 * @param roleName
	 * @return
	 */
	public boolean updateRole(Long roleId,String roleName)throws ServiceException{
		try {
			Role role = roleDAO.findOne(roleId);
			role.setRoleName(roleName);
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
}





